{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b741d333-7f96-44de-bb68-4ab7dd1d6aa6",
   "metadata": {
    "tags": []
   },
   "source": [
    "This script generates binaries for whether a judge is on a panel and whether the judge is in the majority.\n",
    "\n",
    "Pseudocode: \n",
    "1. read in dataset\n",
    "2. create a dictionary with judge names\n",
    "3. Generate panel membership\n",
    "4. Generate majority participation (using panel membership + lack of dissent)\n",
    "5. Generate votes based on majority particiaption\n",
    "6. Identify records that need to be manually reviewed.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "88d7dc13-40f0-4c23-afc1-14c9fcf72f13",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jrantanen/.local/lib/python3.9/site-packages/openpyxl/worksheet/_reader.py:329: UserWarning: Data Validation extension is not supported and will be removed\n",
      "  warn(msg)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "data = pd.read_excel(\"101 Fed. Cir. Cases Database - 11.11.24.xlsx\")\n",
    "# data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "abbd38ab-9125-4361-858a-b0afeec09014",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "judges = [\"Bryson\", \"Chen\", \"Clevenger\", \"Cunningham\", \"Dyk\", \"Gajarsa\", \"Hughes\", \"Linn\", \n",
    "          \"Lourie\", \"Mayer\", \"Moore\", \"Newman\", \"O'Malley\", \"Plager\", \"Prost\", \"Rader\", \"Reyna\", \n",
    "          \"Schall\", \"Stark\", \"Stoll\", \"Taranto\", \"Wallach\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb7c1796-9fb1-41ca-a2d1-648ef04085ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bryson', 'Chen', 'Clevenger', 'Cunningham', 'Dyk', 'Gajarsa', 'Hughes', 'Linn', 'Lourie', 'Mayer', 'Moore', 'Newman', \"O'Malley\", 'Plager', 'Prost', 'Rader', 'Reyna', 'Schall', 'Stark', 'Stoll', 'Taranto', 'Wallach']\n"
     ]
    }
   ],
   "source": [
    "print(judges)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61305ee1-bf40-487d-901c-98be2d66281e",
   "metadata": {},
   "source": [
    "The next section creates a new column in the dataframe that indicates whether the judge is a member of a panel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eec310cb-b9ea-41a8-bb43-89208ae0d05e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for n in judges:\n",
    "    judgename = n\n",
    "\n",
    "    data.loc[data[\"judge1\"] == judgename,\"judge1\"]\n",
    "\n",
    "    for x in range(1,4):\n",
    "        data.loc[(data[\"judge\"+str(x)] == judgename), judgename + \"_panel\"] = 1\n",
    "\n",
    "\n",
    "    data.loc[data[\"judge1\"]== judgename, [\"judge1\", judgename + \"_panel\"]]\n",
    "    data.loc[data[\"judge2\"]== judgename, [\"judge2\", judgename + \"_panel\"]]\n",
    "    data.loc[data[\"judge3\"]== judgename, [\"judge3\", judgename + \"_panel\"]]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35ddf423-8836-499d-9da2-9c7eae16599e",
   "metadata": {},
   "source": [
    "The next section creates a new column in the dataframe that indicates whether the judge joined the majority based on whether the judge was on the panel and did not dissent in whole or in part."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad1a6260-12c0-4bff-9160-ae740f48d1e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "\n",
    "for n in judges:\n",
    "    judgename = n\n",
    "\n",
    "    data.loc[data[judgename + \"_panel\"]==1, judgename + \"_majority\"] = 1\n",
    "    data.loc[(data[\"opinion2Author\"]==judgename) & (data[\"opinion2\"]==\"Dissenting\"), judgename + \"_majority\"] = 0\n",
    "    data.loc[(data[\"opinion3Author\"]==judgename) & (data[\"opinion3\"]==\"Dissenting\"), judgename + \"_majority\"] = 0\n",
    "    data.loc[(data[\"opinion2Author\"]==judgename) & (data[\"opinion2\"]==\"Both\"), judgename + \"_majority\"] = 0\n",
    "    data.loc[(data[\"opinion3Author\"]==judgename) & (data[\"opinion3\"]==\"Both\"), judgename + \"_majority\"] = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30598153-5d3e-4304-9d6a-387c111a98e9",
   "metadata": {},
   "source": [
    "The next step is to create a judge vote field for the 101 issue. \n",
    "\n",
    "Starting with the judges who did not dissent in whole or part."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a61f6962-ee78-462b-a2d7-d8504b5a5aed",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for n in judges:\n",
    "    judgename = n\n",
    "\n",
    "    data.loc[(data[judgename + \"_panel\"]==1) & (data[judgename + \"_majority\"]==1), judgename+\"_101_vote\"] = data[\"PSM_Outcome_CAFC\"]\n",
    "   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11a20682-57c2-4d33-9b0d-fefca278a9b4",
   "metadata": {
    "tags": []
   },
   "source": [
    "Next replace those judges who wrote a second or third opinion with their outcome on that opinion."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "071510db-ae2f-4399-a45f-6d706f8536a3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for n in judges:\n",
    "    judgename = n\n",
    "\n",
    "    data.loc[(data[judgename + \"_panel\"]==1) & (data[\"opinion2Author\"]==judgename), judgename+\"_101_vote\"] = data[\"PSM_SecondOpinion\"]\n",
    "    data.loc[(data[judgename + \"_panel\"]==1) & (data[\"opinion3Author\"]==judgename), judgename+\"_101_vote\"] = data[\"PSM_ThirdOpinion\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01b09cea-2520-4b4e-a7a5-3aa7f6c53340",
   "metadata": {},
   "source": [
    "The next section identifies instances in which a judge is on the panel, but the algorithm did not identify a judge vote. These instances are flagged with review = 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6354392f-ad45-4243-8012-3817cb1c65f3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    477.000000\n",
       "mean       0.018868\n",
       "std        0.136201\n",
       "min        0.000000\n",
       "25%        0.000000\n",
       "50%        0.000000\n",
       "75%        0.000000\n",
       "max        1.000000\n",
       "Name: review, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"review\"] = 0\n",
    "\n",
    "for n in judges:\n",
    "    judgename = n\n",
    "    data.loc[(data[judgename + \"_panel\"]==1) & (data[judgename + \"_101_vote\"].isnull()) & (data[\"PSM_decided\"]==\"2 - Yes\"), \"review\"] = 1\n",
    "                                                \n",
    "data[\"review\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4a2e26b4-4dc0-4fa2-b5b4-3c2a39510fe1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data.to_csv(\"judge votes.csv\", sep='|', index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a5a015c1-0be0-44b2-9167-1f56bf06f065",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done\n"
     ]
    }
   ],
   "source": [
    "print(\"done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fecf2ce2-a972-45ef-9513-066404d025f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fc01430-85b9-4f6b-a9c6-8547de1d59fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9",
   "language": "python",
   "name": "python39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
